package com.cortex.carambola.view;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.ViewFlipper;

import com.cortex.carambola.fetch.FetchImages;
import com.cortex.carambola.fetch.FetchImages.OnStateChanged;
import com.cortex.carambola.summary.CarambolaRequestSummary;
import com.cortex.carambola.summary.CarambolaSummary;
import com.cortex.carambola.utilities.Utilities;
import com.cortex.carambolasummary.R;



public class SummaryOnlineView extends RelativeLayout{

	private TextView tvName,tvSummary;
	private ImageView imgHide,imgShowMoreOptions,imgHideMoreOpetions,imgShare,btnGoSummary;
	private CheckBox cbQuotes;
	private EditText etSentences,etKeywords;
	private TextView tvCharactersNumber;
	private ViewFlipper flipperMoreOpetions;
	private OnActionSummaryViewListener onActionSummaryListener;
	private int widthScreen,heightScreen;
	private CarambolaSummary summary;
	private ScrollView parentScrollView;

	public SummaryOnlineView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SummaryOnlineView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
	}

	private void init(){
		tvName = (TextView)findViewById(R.id.tvSummaryName);
		tvSummary = (TextView)findViewById(R.id.tvSummary);
		flipperMoreOpetions = (ViewFlipper)findViewById(R.id.flipperMoreOptions);
		imgHide = (ImageView)findViewById(R.id.imgHide);
		btnGoSummary = (ImageView)findViewById(R.id.imgGoSummary);
		imgShowMoreOptions = (ImageView)findViewById(R.id.imgShowMoreOptions);
		imgHideMoreOpetions = (ImageView)findViewById(R.id.imgHideMoreOptions);
		imgShare = (ImageView)findViewById(R.id.imgShare);
		etSentences = (EditText)findViewById(R.id.etSentences);
		etKeywords = (EditText)findViewById(R.id.etSummaryKeywords);
		cbQuotes = (CheckBox)findViewById(R.id.cbQuotes);
		tvCharactersNumber = (TextView)findViewById(R.id.tvCharactersNumber);
		// to allow copy of text
		tvSummary.setTextIsSelectable(true);

		initHandlers();
		initView();
		// change font of rowSearch
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fonts/robotoLight.ttf");
		etSentences.setTypeface(tf,Typeface.NORMAL);
		etKeywords.setTypeface(tf,Typeface.NORMAL);
	}
	private void initView(){
		widthScreen= getContext().getResources().getDisplayMetrics().widthPixels;
		heightScreen= getContext().getResources().getDisplayMetrics().heightPixels;
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();

		params.height = heightScreen*33/100; 
	}
	private void initHandlers(){
		imgHide.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				hide();
			}
		});
		imgShowMoreOptions.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Animation show = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in);
				Animation hide = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out);
				flipperMoreOpetions.setInAnimation(show);
				flipperMoreOpetions.setOutAnimation(hide);
				flipperMoreOpetions.showNext();
			}
		});
		imgHideMoreOpetions.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Animation show = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_in);
				Animation hide = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_out);
				flipperMoreOpetions.setInAnimation(show);
				flipperMoreOpetions.setOutAnimation(hide);
				flipperMoreOpetions.showPrevious();
			}
		});
		btnGoSummary.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(onActionSummaryListener != null){
					try{
						if(Integer.parseInt(etSentences.getText().toString()) > 40){
							String message = getResources().getString(R.string.errorUnableMoreh40Sentences);
							Toast.makeText(SummaryOnlineView.this.getContext(), 
									message, Toast.LENGTH_LONG).show();
							return;
						}
					}
					catch(NumberFormatException e){
						String message = getResources().getString(R.string.errorEnterNumberSentences);
						Toast.makeText(SummaryOnlineView.this.getContext(), 
								message, Toast.LENGTH_LONG).show();
						return;
					}
					onActionSummaryListener.onAction(ActionOnSummary.GET_SUMMARY);
					Utilities.showKeyboard(false, v);

				}
			}
		});

		imgShare.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
				sharingIntent.setType("text/plain");
				sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, summary.getName());
				sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, summary.getSummary());
				getContext().startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.shareUsing)));
			}
		});
	}
	public void setOnActionSummaryListener(OnActionSummaryViewListener onActionSummaryListener) {
		this.onActionSummaryListener = onActionSummaryListener;
	}
	public void setParentScroll(ScrollView scrollView){
		// to allow scrolling of child scrollView
		this.parentScrollView = scrollView;
		tvSummary.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				parentScrollView.requestDisallowInterceptTouchEvent(true);
				return false;
			}
		});

	}
	public CarambolaRequestSummary getRequestSummary(){
		CarambolaRequestSummary requsetSummary = new CarambolaRequestSummary();
		requsetSummary.setSentencesNumber(etSentences.getText().toString());
		requsetSummary.setKeywordsNumber(etKeywords.getText().toString());
		requsetSummary.setQuotes(cbQuotes.isChecked());
		requsetSummary.setUrl(getUrl());
		return requsetSummary;
	}
	public String getUrl(){
		return summary.getUrl();
	}
	public void show(CarambolaSummary summary){
		this.summary = summary;
		tvName.setText(summary.getName());
		tvSummary.setText(summary.getSummary());
		etSentences.setText("" + summary.getSentences());
		tvCharactersNumber.setText("" + summary.getCharacters());
		etKeywords.setText("" + summary.sizeKeywords());
		cbQuotes.setChecked(summary.isQuotes());
		setVisibility(View.VISIBLE);
	}
	public void hide(){	
		if(isShown()){
			if(onActionSummaryListener != null){
				onActionSummaryListener.onAction(ActionOnSummary.CLOSED);
			}
			setVisibility(View.GONE);
		}
	}
	
	public void setVisibaleViews(int visibale){
		tvName.setVisibility(visibale);
		tvSummary.setVisibility(visibale);
		imgHide.setVisibility(visibale);
		imgShowMoreOptions.setVisibility(visibale);
		imgHideMoreOpetions.setVisibility(visibale);
		imgShare.setVisibility(visibale);
		btnGoSummary.setVisibility(visibale);
		cbQuotes.setVisibility(visibale);
		etSentences.setVisibility(visibale);
		etKeywords.setVisibility(visibale);
		tvCharactersNumber.setVisibility(visibale);
		flipperMoreOpetions.setVisibility(visibale);
	}
	public interface OnActionSummaryViewListener{
		void onAction(ActionOnSummary action);
	}
	public enum ActionOnSummary{
		GET_SUMMARY,
		CLOSED,
	}

}
